package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.park_management.common.Result;
import com.example.park_management.domain.Admins;
import com.example.park_management.domain.Employees;
import com.example.park_management.domain.Owners;
import com.example.park_management.domain.vo.LoginResultVO;
import com.example.park_management.service.AdminsService;
import com.example.park_management.service.EmployeesService;
import com.example.park_management.service.OwnersService;
import com.example.park_management.util.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
import java.util.UUID;


@RestController
@RequestMapping("/admin")
public class AdminsController {
    //fcc
    @Value("${config.shiro.secret}")
    private String secret;

    @Resource
    AdminsService adminsService;

    @Resource
    EmployeesService employeesService;

    @Resource
    OwnersService ownersService;

    @PostMapping("/login")
    Result login(@RequestBody Admins admins){
        Admins one = adminsService.getOne(new QueryWrapper<Admins>().eq("username", admins.getUsername()).eq("password", admins.getPassword()));
        if (one != null){
            LoginResultVO loginResultVO = new LoginResultVO();
            loginResultVO.setUsername(one.getUsername());
            loginResultVO.setUsername(one.getPassword());
            loginResultVO.setToken(JwtUtil.sign(one.getAdminId().toString(),secret,"admin"));
            return Result.success(loginResultVO);
        }
        return Result.fail("账号或密码错误");
    }

    @GetMapping("/getInfo")
    Result getInfo(String token){
        Integer userId = Integer.parseInt(JwtUtil.getUsername(token));
        String role = JwtUtil.getRole(token);
        if (role.equals("admin")){
            Admins one = adminsService.getById(userId);
            one.setRoles(Collections.singletonList("admin"));
            return Result.success(one);
        }
        if (role.equals("employees")){
            Employees one = employeesService.getById(userId);
            one.setRoles(Collections.singletonList("employees"));
            return Result.success(one);
        }
        if (role.equals("owners")){
            Owners one = ownersService.getById(userId);
            one.setRoles(Collections.singletonList("owners"));
            return Result.success(one);
        }
        return Result.fail();
    }

    @PutMapping("/")
    public Result update(@RequestBody Admins admins) {
        try{
            if (adminsService.updateById(admins)){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID() + "." + file.getOriginalFilename().split("\\.")[1];
        //获取项目 发布路径
        String path = System.getProperty("user.dir") + "\\target\\classes\\static\\";
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File dest = new File(path + fileName);
        Files.copy(file.getInputStream(), dest.toPath());
        //复制一份到静态资源文件夹
        File dest2 = new File(System.getProperty("user.dir") + "\\src\\main\\resources\\static\\" + fileName);
        Files.copy(file.getInputStream(), dest2.toPath());
        return Result.success("http://127.0.0.1:8080/static/"+fileName);
    }

}
